﻿using System;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace HYFloorPipeModule
{
	public class ArcPtPair
	{
		public ArcPtPair()
		{
			this.m_bIn = false;
			this.m_bIsLoopLast = false;
		}

		public ArcPtPair(ArcPtPair src)
		{
			this.m_ptStart = src.m_ptStart;
			this.m_ptMid = src.m_ptMid;
			this.m_ptEnd = src.m_ptEnd;
			this.m_bIn = src.m_bIn;
			this.m_bIsLoopLast = src.m_bIsLoopLast;
		}

		public ArcPtPair(XYZ s, XYZ m, XYZ e, bool bIn, bool bLoopLast = false)
		{
			this.m_ptStart = s;
			this.m_ptMid = m;
			this.m_ptEnd = e;
			this.m_bIn = bIn;
			this.m_bIsLoopLast = bLoopLast;
		}

		public bool IsLoopLast
		{
			get
			{
				return this.m_bIsLoopLast;
			}
			set
			{
				this.m_bIsLoopLast = value;
			}
		}

		public bool IsIn
		{
			get
			{
				return this.m_bIn;
			}
			set
			{
				this.m_bIn = value;
			}
		}

		public XYZ Start
		{
			get
			{
				return this.m_ptStart;
			}
			set
			{
				this.m_ptStart = value;
			}
		}

		public XYZ Mid
		{
			get
			{
				return this.m_ptMid;
			}
			set
			{
				this.m_ptMid = value;
			}
		}

		public XYZ End
		{
			get
			{
				return this.m_ptEnd;
			}
			set
			{
				this.m_ptEnd = value;
			}
		}

		public void ReInitByTwoOrderPerPair(PointPair ppF, PointPair ppS, double dRadius, bool bLoopLast = false)
		{
			this.m_bIsLoopLast = bLoopLast;
			XYZ xyz = ppF.AsVector().Normalize();
			XYZ xyz2 = ppS.AsVector().Normalize();
			Line line = ppF.CreateLine();
			line.MakeUnbound();
			XYZ xyzpoint = line.Project(ppS.First).XYZPoint;
			if (xyzpoint != null)
			{
				this.m_ptStart = xyzpoint - xyz * dRadius;
				this.m_ptEnd = xyzpoint + xyz2 * dRadius;
				XYZ xyz3 = this.m_ptStart + xyz2 * dRadius;
				XYZ xyz4 = (xyz - xyz2).Normalize();
				this.m_ptMid = xyz3 + xyz4 * dRadius;
				this.m_bIn = ppF.IsIn;
			}
		}

		public void ReInitByTwoPerPair(PointPair ppF, PointPair ppS, double dRadius, bool bLoopLast = false)
		{
			FloorPipeTools floorPipeTools = new FloorPipeTools();
			this.m_bIsLoopLast = bLoopLast;
			Line line = ppF.CreateLine();
			Line line2 = ppS.CreateLine();
			XYZ intersectionPoint = floorPipeTools.GetIntersectionPoint(line, line2);
			XYZ xyz = (floorPipeTools.GetFarEndPointfromBasePiont(line, intersectionPoint) - intersectionPoint).Normalize();
			XYZ xyz2 = (floorPipeTools.GetFarEndPointfromBasePiont(line2, intersectionPoint) - intersectionPoint).Normalize();
			double num = YJKRevitTools.GetTwoVectorAngle(xyz, xyz2, new XYZ(0.0, 0.0, 1.0));
			if (num > Math.PI)
			{
				num = Math.PI*2 - num;
			}
			double num2 = dRadius / Math.Tan(0.5 * num);
			XYZ xyz3 = intersectionPoint + xyz * num2;
			XYZ xyz4 = intersectionPoint + xyz2 * num2;
			line = Line.CreateBound(floorPipeTools.GetFarEndPointfromBasePiont(line, intersectionPoint), xyz3);
			line2 = Line.CreateBound(floorPipeTools.GetFarEndPointfromBasePiont(line2, intersectionPoint), xyz4);
			XYZ vectorByHorizontal = floorPipeTools.GetVectorByHorizontal(xyz);
			XYZ vectorByHorizontal2 = floorPipeTools.GetVectorByHorizontal(xyz2);
			Line line3 = Line.CreateUnbound(xyz3, vectorByHorizontal);
			Line line4 = Line.CreateUnbound(xyz4, vectorByHorizontal2);
			XYZ intersectionPoint2 = floorPipeTools.GetIntersectionPoint(line3, line4, true);
			XYZ ptMid = intersectionPoint2 + dRadius * (intersectionPoint - intersectionPoint2).Normalize();
			this.m_ptStart = xyz3;
			this.m_ptMid = ptMid;
			this.m_ptEnd = xyz4;
			this.m_bIn = ppF.IsIn;
		}

		public Arc CreateArc()
		{
			return Arc.Create(this.m_ptStart, this.m_ptEnd, this.m_ptMid);
		}

		public override string ToString()
		{
			return string.Concat(new string[]
			{
				"Start Point:",
				this.m_ptStart.ToString(),
				"\nMid Point:",
				this.m_ptMid.ToString(),
				"\nEnd Point:",
				this.m_ptEnd.ToString()
			});
		}

		public void DoExchange()
		{
			XYZ ptEnd = this.m_ptEnd;
			this.m_ptEnd = this.m_ptStart;
			this.m_ptStart = ptEnd;
		}

		public void Retract(double dFirst, double dEnd)
		{
			XYZ xyz = (this.m_ptEnd - this.m_ptStart).Normalize();
			this.m_ptStart += xyz * dFirst;
			this.m_ptEnd -= xyz * dEnd;
			XYZ xyz2 = (this.m_ptEnd + this.m_ptStart) * 0.5;
			Arc arc = this.CreateArc();
			this.m_ptStart = arc.Project(this.m_ptStart).XYZPoint;
			xyz2 = arc.Project(xyz2).XYZPoint;
			this.m_ptEnd = arc.Project(this.m_ptEnd).XYZPoint;
		}

		private XYZ m_ptStart;

		private XYZ m_ptMid;

		private XYZ m_ptEnd;

		private bool m_bIn;

		private bool m_bIsLoopLast;
	}
}
